<?php

namespace App\Http\Models\FolderResource;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class UserFolder
 *
 * @method self filterLevel(int $root_folder_id = UserFolder::ROOT_FOLDER_ID)
 *
 * @package App\Http\Models\FolderResource
 */
class UserFolder extends Model
{
    const ROOT_FOLDER_ID = 0;
    const DEFAULT_LEVEL_DEEP = 1;

    protected $table = 'user_folders';

    protected static $unguarded = true;

    /**
     * @param Builder $builder
     * @param int $super_id
     * @return Builder
     */
    public function scopeFilterLevel(Builder $builder, $super_id = self::ROOT_FOLDER_ID )
    {
        return $builder->where('super_id', $super_id);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function subFolders()
    {
        return $this->hasMany(self::class, 'super_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function supFolder()
    {
        return $this->belongsTo(self::class, 'super_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function resources()
    {
        return $this->hasMany(UserResource::class, 'user_folder_id');
    }

}

